package com.bolt.admin.config;

import com.bolt.common.io.resource.ResourceUtil;
import lombok.Getter;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import javax.annotation.PostConstruct;
import java.net.URL;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.util.Enumeration;

@Getter
@Component
public class RsaProperties {

    public static String privateKey;

    @PostConstruct
    public void initPrivateKey(){
        try{
            BASE64Encoder encoder = new BASE64Encoder();
            URL url = ResourceUtil.getResource("jwt.jks");
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load(url.openStream(), "michao123".toCharArray());
            Enumeration<String> aliases = keyStore.aliases();
            String alias = null;
            while (aliases.hasMoreElements()) {
                alias = aliases.nextElement();
            }
            PrivateKey key = (PrivateKey) keyStore.getKey(alias, "michao123".toCharArray());
            RsaProperties.privateKey = encoder.encode(key.getEncoded());
        }catch (Exception ex){

        }

    }

}

 